<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }

        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>运维中心
            <small>用户管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>动态维护</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="用户名/电话" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="findPage2()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="username" label="用户名" align="center"></el-table-column>
                <el-table-column label="密码" align="center">
                    <template slot-scope="scope">
                        <span>******</span>
                    </template>
                </el-table-column>

                <el-table-column label="性别" align="center">
                    <template slot-scope="scope">
                        <span>{{scope.row.gender == '1' ? '男' : '女'}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="birthday" label="生日" align="center"></el-table-column>
                <el-table-column prop="telephone" label="电话" align="center"></el-table-column>


                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)" size="small">

                            <span><i class="el-icon-edit"></i><span class="icon-name">编辑</span></span>

                        </el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)" size="small">

                            <span><i class="el-icon-delete"></i><span class="icon-name">删除</span></span>

                        </el-button>
                    </template>
                </el-table-column>
            </el-table>


            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="用户名">
                                                <el-input
                                                        placeholder="一经提交, 无法修改"
                                                        v-model="formData.username" clearable/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="密码">
                                                <el-input
                                                        placeholder="请输入密码"
                                                        v-model="formData.password" clearable/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="性别">

                                                <el-radio v-model="formData.gender" label="1">男</el-radio>
                                                <el-radio v-model="formData.gender" label="2">女</el-radio>

                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="生日">

                                                <el-col>
                                                    <el-date-picker type="date" placeholder="选择日期"
                                                                    v-model="formData.birthday"
                                                                    style="width: 100%;"></el-date-picker>
                                                </el-col>

                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="电话">
                                                <el-input
                                                        placeholder="请输入电话"
                                                        v-model="formData.telephone" clearable/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="状态">
                                                <el-input
                                                        placeholder="请输入状态"
                                                        v-model="formData.station" clearable/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">

                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="备注">

                                                <el-input
                                                        type="textarea"
                                                        :autosize="{ minRows: 2, maxRows: 4}"
                                                        placeholder="请输入备注"
                                                        v-model="formData.remark" clearable>
                                                </el-input>

                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="角色信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>角色名称</th>
                                            <th>角色描述</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.description}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑" :visible.sync="dialogFormVisible4Edit">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="用户名">

                                                <el-input
                                                        v-model="formData.username"
                                                        :disabled="true">
                                                </el-input>

                                            </el-form-item>

                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="密码">


                                                <el-input
                                                        placeholder="请输入密码"
                                                        v-model="formData.password"
                                                        show-password>
                                                </el-input>

                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="性别">

                                                <el-radio v-model="formData.gender" label="1" >男</el-radio>
                                                <el-radio v-model="formData.gender" label="2" >女</el-radio>

                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="生日">

                                                <el-col>
                                                    <el-date-picker type="date" placeholder="选择日期"
                                                                    v-model="formData.birthday"
                                                                    style="width: 100%;"></el-date-picker>
                                                </el-col>

                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="电话">


                                                <el-input
                                                        placeholder="请输入电话"
                                                        v-model="formData.telephone"
                                                        clearable>
                                                </el-input>

                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="状态">

                                                <el-input
                                                        placeholder="请输入状态"
                                                        v-model="formData.station"
                                                        clearable>
                                                </el-input>

                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">

                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="备注">
                                                <el-input
                                                        type="textarea"
                                                        :autosize="{ minRows: 2, maxRows: 4}"
                                                        placeholder="请填充备注"
                                                        v-model="formData.remark">
                                                </el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="角色信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>角色名称</th>
                                            <th>角色描述</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.description}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="update()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    var vue = new Vue({
        el: '#app',
        data: {

            activeName: 'first',//添加/编辑窗口Tab标签名称
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 5,
                total: 1,
                queryString: null,
            },
            dataList: [],//列表数据
            formData: {},//表单数据
            tableData: [],//添加表单窗口中角色列表数据
            roleIds: [],//添加表单窗口中角色复选框对应id
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisible4Edit: false
        },
        created() {
            this.findPage();
        },
        methods: {

            //添加
            handleAdd() {
                //把用户数据(formData)和选择的角色的id(roleIds)提交到服务器
                axios.post("/user/add.do?roleIds=" + this.roleIds, this.formData).then(response => {
                    this.$message({
                        message: response.data.message,
                        type: response.data.flag ? 'success' : 'error'
                    });
                    //新增成功, 关闭dialog, 查询展示
                    if (response.data.flag) {
                        this.dialogFormVisible = false;
                        this.findPage();
                    }
                });

            },
            //分页查询
            findPage() {
                //分页参数
                var params = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };

                //请求后台
                axios.post("/user/findPage.do", params).then(response => {

                    //为模型数据赋值,基于VUE的双向绑定展示到页面
                    this.pagination.total = response.data.data.total;
                    this.dataList = response.data.data.rows;

                });
            },
            /******************* 分页查询 *******************/
            findPage2() {
                this.pagination.currentPage = 1;
                this.findPage();
            },
            // 重置表单
            resetForm() {
                this.activeName = 'first';
                this.formData = {};
                this.tableData = [];
                this.roleIds = [];
            },
            // 弹出添加窗口
            handleCreate() {

                this.resetForm();
                //获得所有的角色数据
                axios.get("/role/findAll.do").then(response => {
                    if (response.data.flag) {
                        //角色的数据绑定
                        this.tableData = response.data.data;
                        //弹出dialog
                        this.dialogFormVisible = true;
                    }

                });

            },
            //切换页码
            handleCurrentChange(currentPage) {
                //把currentPage重新赋值
                this.pagination.currentPage = currentPage;
                //调用findPage()
                this.findPage();
            },

            // 弹出编辑窗口
            handleUpdate(row) {
                this.activeName = 'first';
                var id = row.id;
                //1 根据用户的id查询出用户对象
                axios.get("/user/findById.do?id=" + id).then(response => {
                    if (response.data.flag) {
                        this.formData = response.data.data;
                        //2查询所有的角色列表
                        axios.get("/role/findAll.do").then(response => {
                            if (response.data.flag) {
                                this.tableData = response.data.data;
                                //3 根据检查组的id查询出当前检查组关联的检查项的Id(查询中间表t_checkgroup_checkitem) checkitemIds
                                axios.get("/user/findRoleIdsByUserId.do?userId=" + id).then(response => {
                                    if (response.data.flag) {
                                        this.roleIds = response.data.data;
                                        //4 弹出编辑窗口
                                        this.dialogFormVisible4Edit = true;
                                    }
                                });


                            }
                        });


                    }
                });


            },

            //修改
            update() {

                //通过axios把修改后的基本信息(formData)和修改后角色id(roleIds) 提交到UserController
                axios.post("/user/edit.do?roleIds=" + this.roleIds, this.formData).then(response => {
                    this.$message({
                        message: response.data.message,
                        type: response.data.flag ? 'success' : 'error'
                    });
                    //是否更新成功
                    if (response.data.flag) {
                        this.dialogFormVisible4Edit = false;
                        this.findPage();
                    }
                });

            },
            // 删除
            handleDelete(row) {
                //1. 点击删除, 弹出确定框
                this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //2. 点击了确定,发送axios请求UserController, 携带id, 获得响应结果提示
                    axios.get("/user/delete.do?id=" + row.id).then(response => {
                        //提示
                        this.$message({
                            message: response.data.message,
                            type: response.data.flag ? 'success' : 'error'
                        });
                        //删除成功了, 查询展示
                        this.findPage();
                    });
                });

            },

        }
    })
</script>
</html>
